package com.tjec.project.projectShowLibrary.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.tjec.common.constant.Constants;
import com.tjec.common.utils.StringUtils;
import com.tjec.project.projectShowLibrary.domain.BmsProjectShowLibrary;
import com.tjec.project.projectShowLibrary.domain.BmsProjectShowOper;
import com.tjec.project.projectShowLibrary.mapper.BmsProjectShowLibraryMapper;
import com.tjec.project.projectShowLibrary.mapper.BmsProjectShowOperMapper;
import com.tjec.project.projectShowLibrary.service.IBmsProjectShowLibraryService;
import com.tjec.project.projectShowLibrary.service.IBmsProjectShowOperService;
import com.tjec.project.projectWorkbench.domain.BmsProject;
import com.tjec.project.projectWorkbench.mapper.BmsProjectMapper;
import com.tjec.project.system.domain.BmsAttachment;
import com.tjec.project.system.mapper.BmsAttachmentMapper;

/**
 * 项目展示库操作记录Service业务层处理
 * 
 * @author tjec
 * @date 2021-05-10
 */
@Service
public class BmsProjectShowLibraryServiceImpl implements IBmsProjectShowLibraryService 
{

	@Autowired
	private BmsProjectShowLibraryMapper bmsProjectShowLibraryMapper;
	
	@Autowired
	private BmsAttachmentMapper bmsAttachmentMapper;
	
	@Autowired
	private BmsProjectMapper bmsProjectMapper;
	
	@Value("${sftp.http}")
    private String http;
    @Value("${sftp.ip}")
    private String ip;
	
	@Override
	public List<BmsProjectShowLibrary> selectBmsProjectShowLibraryList(BmsProjectShowLibrary bmsProjectShowLibrary) {
		List<BmsProjectShowLibrary> list = bmsProjectShowLibraryMapper.selectBmsProjectShowLibraryList(bmsProjectShowLibrary);
		if(list!=null && list.size()>0) {
			for (BmsProjectShowLibrary bmsProjectShowLibrary2 : list) {
				String mainPic = bmsProjectShowLibrary2.getShowProjectMain();
				if(StringUtils.isNotBlank(mainPic)) {
					BmsAttachment att = bmsAttachmentMapper.selectBmsAttachmentById(mainPic);
					if(att!=null) {
						bmsProjectShowLibrary2.setShowProjectMain(http + ip +Constants.RESOURCE_PREFIX + att.getStoragePath());
					}
				}
				String pictures = bmsProjectShowLibrary2.getShowProjectPictures();
				if(StringUtils.isNotBlank(pictures)) {//如果项目展示图片不为空时，查询出照片地址
					List<String> pics = new ArrayList<String>();
					String[] attIds = pictures.split(";");
					for (String string : attIds) {
						BmsAttachment att = bmsAttachmentMapper.selectBmsAttachmentById(string);
						pics.add(http + ip +Constants.RESOURCE_PREFIX + att.getStoragePath());
					}
					bmsProjectShowLibrary2.setPictures(pics);
				}
			}
		}
		return list;
	}
	
	@Override
	public List<BmsProjectShowLibrary> selectBmsProjectShowLibraryListNo(BmsProjectShowLibrary bmsProjectShowLibrary) {
		List<BmsProjectShowLibrary> list = bmsProjectShowLibraryMapper.selectBmsProjectShowLibraryListNo(bmsProjectShowLibrary);
		if(list!=null && list.size()>0) {
			for (BmsProjectShowLibrary bmsProjectShowLibrary2 : list) {
				String mainPic = bmsProjectShowLibrary2.getShowProjectMain();
				if(StringUtils.isNotBlank(mainPic)) {
					BmsAttachment att = bmsAttachmentMapper.selectBmsAttachmentById(mainPic);
					if(att!=null) {
						bmsProjectShowLibrary2.setShowProjectMain(http + ip +Constants.RESOURCE_PREFIX + att.getStoragePath());
					}
				}
				String pictures = bmsProjectShowLibrary2.getShowProjectPictures();
				if(StringUtils.isNotBlank(pictures)) {//如果项目展示图片不为空时，查询出照片地址
					List<String> pics = new ArrayList<String>();
					String[] attIds = pictures.split(";");
					for (String string : attIds) {
						BmsAttachment att = bmsAttachmentMapper.selectBmsAttachmentById(string);
						pics.add(http + ip +Constants.RESOURCE_PREFIX + att.getStoragePath());
					}
					bmsProjectShowLibrary2.setPictures(pics);
				}
			}
		}
		return list;
	}
	
	@Override
	public List<BmsProjectShowLibrary> selectBmsProjectShowLibraryListDefault(BmsProjectShowLibrary bmsProjectShowLibrary) {
		List<BmsProjectShowLibrary> list = bmsProjectShowLibraryMapper.selectBmsProjectShowLibraryListDefault(bmsProjectShowLibrary);
		if(list!=null && list.size()>0) {
			for (BmsProjectShowLibrary bmsProjectShowLibrary2 : list) {
				String mainPic = bmsProjectShowLibrary2.getShowProjectMain();
				if(StringUtils.isNotBlank(mainPic)) {
					BmsAttachment att = bmsAttachmentMapper.selectBmsAttachmentById(mainPic);
					if(att!=null) {
						bmsProjectShowLibrary2.setShowProjectMain(http + ip +Constants.RESOURCE_PREFIX + att.getStoragePath());
					}
				}
				String pictures = bmsProjectShowLibrary2.getShowProjectPictures();
				if(StringUtils.isNotBlank(pictures)) {//如果项目展示图片不为空时，查询出照片地址
					List<String> pics = new ArrayList<String>();
					String[] attIds = pictures.split(";");
					for (String string : attIds) {
						BmsAttachment att = bmsAttachmentMapper.selectBmsAttachmentById(string);
						pics.add(http + ip +Constants.RESOURCE_PREFIX + att.getStoragePath());
					}
					bmsProjectShowLibrary2.setPictures(pics);
				}
			}
		}
		return list;
	}
	
	@Override
	public List<BmsProjectShowLibrary> selectBmsProjectShowLibraryListSecurity(BmsProjectShowLibrary bmsProjectShowLibrary) {
		List<BmsProjectShowLibrary> list = bmsProjectShowLibraryMapper.selectBmsProjectShowLibraryListSecurity(bmsProjectShowLibrary);
		if(list!=null && list.size()>0) {
			for (BmsProjectShowLibrary bmsProjectShowLibrary2 : list) {
				String mainPic = bmsProjectShowLibrary2.getShowProjectMain();
				if(StringUtils.isNotBlank(mainPic)) {
					BmsAttachment att = bmsAttachmentMapper.selectBmsAttachmentById(mainPic);
					if(att!=null) {
						bmsProjectShowLibrary2.setShowProjectMain(http + ip +Constants.RESOURCE_PREFIX + att.getStoragePath());
					}
				}
				String pictures = bmsProjectShowLibrary2.getShowProjectPictures();
				if(StringUtils.isNotBlank(pictures)) {//如果项目展示图片不为空时，查询出照片地址
					List<String> pics = new ArrayList<String>();
					String[] attIds = pictures.split(";");
					for (String string : attIds) {
						BmsAttachment att = bmsAttachmentMapper.selectBmsAttachmentById(string);
						pics.add(http + ip +Constants.RESOURCE_PREFIX + att.getStoragePath());
					}
					bmsProjectShowLibrary2.setPictures(pics);
				}
			}
		}
		return list;
	}

	@Override
	public int lockOrUnlockProject(BmsProjectShowLibrary bmsProjectShowLibrary) {
		return bmsProjectShowLibraryMapper.lockOrUnlockProject(bmsProjectShowLibrary);
	}

	@Override
	public BmsProjectShowLibrary getProjectShowDetail(String guid) {
		BmsProjectShowLibrary res = bmsProjectShowLibraryMapper.getProjectShowDetail(guid);
		String mainPic = res.getShowProjectMain();
		if(StringUtils.isNotBlank(mainPic)) {
			BmsAttachment att = bmsAttachmentMapper.selectBmsAttachmentById(mainPic);
			if(att!=null) {
				res.setShowProjectMain(http + ip +Constants.RESOURCE_PREFIX + att.getStoragePath());
			}
		}
		String pictures = res.getShowProjectPictures();
		if(StringUtils.isNotBlank(pictures)) {//如果项目展示图片不为空时，查询出照片地址
			List<String> pics = new ArrayList<String>();
			String[] attIds = pictures.split(";");
			for (String string : attIds) {
				BmsAttachment att = bmsAttachmentMapper.selectBmsAttachmentById(string);
				pics.add(http + ip +Constants.RESOURCE_PREFIX + att.getStoragePath());
			}
			res.setPictures(pics);
		}
		return res;
	}

	@Override
	public List<BmsProject> selectBmsProjectList(BmsProject bmsProject) {
		bmsProject.setShowProject("0");
		return bmsProjectMapper.selectBmsProjectList(bmsProject);
	}

	@Override
	public int updateProjectIsShow(String[] guids) {
		return bmsProjectShowLibraryMapper.updateProjectIsShow(guids);
	}
	
	
}
